#include "thumbnail.h"
#include <QDebug>

Thumbnail::Thumbnail(PDFKit *p, QWidget *parent):pdf(p),QWidget(parent)
{


    QListView *listView = new QListView();
    listView->setViewMode(QListView::IconMode);
    listView->setSpacing(10);
    listView->setIconSize(QSize(150, 300));
    listView->setModelColumn(1);
    listView->setMovement(QListView::Static);
    listView->setResizeMode(QListView::Adjust);

    model = new QStandardItemModel();
    listView->setModel(model);

    QVBoxLayout *mainlayout = new QVBoxLayout();
    mainlayout->addWidget(listView);
    mainlayout->setContentsMargins(1,1,1,1);
    setLayout(mainlayout);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Thumbnail::timeOut);
    timer->start(1000);

    size = pdf->get_totalPage();
    currentPage = 0;
}


void Thumbnail::timeOut()
{
    if(currentPage >= size)
    {
        timer->stop();
        delete timer;
        timer = nullptr;
    }
    else
    {
        QImage image = pdf->get_actual_image(currentPage);

        QPixmap pixmap = QPixmap::fromImage(image);
        QString strPageNum = QString::number(currentPage+1);
        QStandardItem *item = new QStandardItem(QIcon(pixmap), strPageNum);
        model->appendRow(item);
        currentPage++;
    }
}
